<template>
  <el-dialog
    :title="!dataForm.authenId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="经纪人id" prop="authenBrokerId">
      <el-input v-model="dataForm.authenBrokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="authenName">
      <el-input v-model="dataForm.authenName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="authenSex">
      <el-input v-model="dataForm.authenSex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="证件照" prop="authenCertificateImg">
      <el-input v-model="dataForm.authenCertificateImg" placeholder="证件照"></el-input>
    </el-form-item>
    <el-form-item label="服务城市" prop="authenCity">
      <el-input v-model="dataForm.authenCity" placeholder="服务城市"></el-input>
    </el-form-item>
    <el-form-item label="所属公司



所属公司" prop="authenBoss">
      <el-input v-model="dataForm.authenBoss" placeholder="所属公司



所属公司"></el-input>
    </el-form-item>
    <el-form-item label="身份证照片" prop="authenIdcardImg">
      <el-input v-model="dataForm.authenIdcardImg" placeholder="身份证照片"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          authenId: 0,
          authenBrokerId: '',
          authenName: '',
          authenSex: '',
          authenCertificateImg: '',
          authenCity: '',
          authenBoss: '',
          authenIdcardImg: ''
        },
        dataRule: {
          authenBrokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ],
          authenName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          authenSex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          authenCertificateImg: [
            { required: true, message: '证件照不能为空', trigger: 'blur' }
          ],
          authenCity: [
            { required: true, message: '服务城市不能为空', trigger: 'blur' }
          ],
          authenBoss: [
            { required: true, message: '所属公司



所属公司不能为空', trigger: 'blur' }
          ],
          authenIdcardImg: [
            { required: true, message: '身份证照片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.authenId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.authenId) {
            this.$http({
              url: this.$http.adornUrl(`/other/authentication/info/${this.dataForm.authenId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.authenBrokerId = data.authentication.authenBrokerId
                this.dataForm.authenName = data.authentication.authenName
                this.dataForm.authenSex = data.authentication.authenSex
                this.dataForm.authenCertificateImg = data.authentication.authenCertificateImg
                this.dataForm.authenCity = data.authentication.authenCity
                this.dataForm.authenBoss = data.authentication.authenBoss
                this.dataForm.authenIdcardImg = data.authentication.authenIdcardImg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/other/authentication/${!this.dataForm.authenId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'authenId': this.dataForm.authenId || undefined,
                'authenBrokerId': this.dataForm.authenBrokerId,
                'authenName': this.dataForm.authenName,
                'authenSex': this.dataForm.authenSex,
                'authenCertificateImg': this.dataForm.authenCertificateImg,
                'authenCity': this.dataForm.authenCity,
                'authenBoss': this.dataForm.authenBoss,
                'authenIdcardImg': this.dataForm.authenIdcardImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
